package org.rqb.panel;
import lombok.extern.slf4j.Slf4j;
import org.rqb.frame.MyFrame;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class SignCenterPanel extends javax.swing.JPanel implements MouseListener, MouseMotionListener {

    private MyFrame frame;
    private JPanel cardPanel;
    private CardLayout cardLayout;

    private List<List<Point>> strokes = new ArrayList<>();
    private List<Point> stroke = new ArrayList<>();
    private List<List<Point>> undoList = new ArrayList<>();


    public SignCenterPanel(JPanel cardPanel, CardLayout cardLayout, MyFrame myFrame) {
        this.frame = myFrame;
        this.cardPanel = cardPanel;
        this.cardLayout = cardLayout;
        panelInit();
        layoutInit();
        listenerInit();
    }

    private void panelInit() {
        this.setBackground(Color.WHITE);
    }

    private void layoutInit() {

    }


    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;
        g2d.setComposite(AlphaComposite.SrcOver);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(Color.BLACK);
        g2d.setStroke(new BasicStroke(2));  // 设置线条粗细

        // 绘制已经完成的笔画
        for (List<Point> s : strokes) {
            for (int j = 1; j < s.size(); j++) {
                Point p1 = s.get(j - 1);
                Point p2 = s.get(j);
                g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
            }
        }

        // 绘制当前正在进行的笔画
        for (int j = 1; j < stroke.size(); j++) {
            Point p1 = stroke.get(j - 1);
            Point p2 = stroke.get(j);
            g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
        }
    }


    private void listenerInit() {
        this.addMouseListener(this);
        this.addMouseMotionListener(this);

    }

    @Override
    public void mouseClicked(MouseEvent e) {}

    @Override
    public void mousePressed(MouseEvent e) {
        stroke.add(e.getPoint());
        repaint();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        strokes.add(stroke);
        stroke = new ArrayList<>();
        repaint();
    }

    @Override
    public void mouseEntered(MouseEvent e) {}

    @Override
    public void mouseExited(MouseEvent e) {}

    @Override
    public void mouseDragged(MouseEvent e) {
        stroke.add(e.getPoint());
        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e) {}


    public void undoLast() {
        if (strokes.size() > 0) {
            List<Point> undoStrokes = strokes.remove(strokes.size() - 1);
            undoList.add(undoStrokes);
        }
        repaint();
    }

    public void redoLast() {
        if (undoList.size() > 0) {
            strokes.add(undoList.remove(undoList.size() - 1));
        }
        repaint();
    }

    public void reset() {
        strokes.clear();
        undoList.clear();
        stroke.clear();
        repaint();
    }
    public void saveSignatureToFile(String dir, String fileName) {
        int width = getWidth();
        int height = getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2d = image.createGraphics();
        g2d.setComposite(AlphaComposite.Clear);
        paintComponent(g2d);
        g2d.dispose();

        File dirFile = new File(dir);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        File file = new File(dir, fileName);

        
        try {
            String suffix = fileName.split("\\.")[1];
            ImageIO.write(image, suffix, file);
            JOptionPane.showMessageDialog(this, "保存文件成功");
        } catch (IOException e) {
            log.error("保存文件失败", e);
            JOptionPane.showMessageDialog(this, "保存文件失败");
        }
    }
}
